﻿using AntDesign;
using EmployeeLeaveSystem.Shared.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace EmployeeLeaveSystem.Server.Controllers.WorkController {
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WorkController : ControllerBase {
        readonly EpMainContext _Context;
        public WorkController(EpMainContext context) { _Context = context; }
        [HttpGet]
        public ActionResult<NowRules?> GetLatestRuleForDepartment(int Id) {
            return _Context.NowRules.OrderByDescending(x => x.Id).Where(x => x.DepartmentId.Equals(Id)).FirstOrDefault();
        }
        [HttpGet]
        public ActionResult<AddWork?> GetWorkInfo(int Id) {
            return _Context.AddWorks.Where(x => x.Id == Id).FirstOrDefault();
        }
        [HttpGet]
        public ActionResult<List<AddWork>?> GetWorkInfos(int usrId) {
            return _Context.AddWorks.Where(x => x.StaffId.Equals(usrId)).ToList();
        }
        [HttpPost]
        public ActionResult AddWorkInfo(AddWork work) {
            work.Created = DateTime.Now;
            work.AcceptFlag = AcceptFlag.审核中;
            _Context.AddWorks.Add(work);
            _Context.SaveChanges();
            return Ok();
        }
        [HttpPost]
        public ActionResult UpdateWorkInfo(AddWork work) {
            _Context.AddWorks.Update(work);
            _Context.SaveChanges();
            return Ok();
        }
        [HttpGet]
        public ActionResult DeleteWorkInfo(int Id) {
            _Context.AddWorks.Remove(new AddWork() { Id = Id });
            _Context.SaveChanges();
            return Ok();
        }
        // 获取Work的相信信息
        public AddWork GetAddWorkById(int id) {
            return _Context.AddWorks.Where(x => x.Id.Equals(id)).Include(x => x.Staff).Include(x => x.AcceptStaff).First();
        }
        // 获取审核人需要审核的信息
        public List<AddWork> GetWorkListManagerId(int id) {
            return _Context.AddWorks.Where(x => x.AcceptStaffId == id).Include(x => x.Staff).ToList();
        }
        // 更新信息
        public ActionResult Update(AcceptFlag acceptFlag, int id) {
            var enty = _Context.AddWorks.Where(x => x.Id.Equals(id)).FirstOrDefault();
            enty.AcceptFlag = acceptFlag;
            _Context.AddWorks.Update(enty);
            _Context.SaveChanges();
            return Ok();
        }
    }
}
